package 第五周;

/*
 * @author  YxinMiracle
 * @date  2022-05-03 21:17
 * @Gitee: https://gitee.com/yxinmiracle
 */

public class 数组的相对排序 {

    // 非比较的排序算法
    public int[] relativeSortArray(int[] arr1, int[] arr2) {
        int index = 0;
        int[] result = new int[arr1.length];
        int[] count = new int[1001];

        for (int num : arr1) {
            count[num] ++;
        }

        for (int num : arr2) {
            while (count[num] > 0){
                result[index++] = num;
                count[num] --;
            }
        }

        for (int i = 0; i < 1001; i++) {
            while (count[i] > 0){
                result[index++] = i;
                count[i] --;
            }
        }
        return result;
    }

}
